package sk.naive.talker.server;

import sk.naive.talker.*;
import sk.naive.talker.persistence.PersistentPropertyStoreImpl;
import sk.naive.talker.util.PropertyConfiguration;
import sk.naive.talker.props.*;

import java.util.*;

/**
 * Base implementation of user object on the kernel side.
 * <p>
 * Concrete subclasses are provided for offline and online user.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.41 $ $Date: 2007-05-07 10:34:20 $
 */
public abstract class UserProxy
	implements User,
	Comparable
{
	private PersistentPropertyStoreImpl store;

	protected UserProxy(Integer id, PropertyConfiguration pc) {
		store = new PersistentPropertyStoreImpl(id, pc);
	}

	public Set<String> getSettable() {
		return store.getSettable();
	}

	public String getLogin() {
		return (String) getProperties().get(UPROP_LOGIN);
	}

	public String getLang() {
		return (String) getProperties().get(UPROP_LANG);
	}

	public Object get(String key) throws PropertyStoreException {
		Object rval = getProperties().get(key);
		if (rval == null) {
			return getProperty(key).defaultValue();
		}
		return rval;
	}

	public int compareTo(Object o) {
		return getLogin().compareTo(((User) o).getLogin());
	}

	public Integer getId() {
		return store.getId();
	}

	public void setStoredValue(String key, String value) throws PropertyStoreException {
		store.setStoredValue(key, value);
	}

	public void set(String property, Object value) throws PropertyStoreException {
		store.set(property, value);
	}

	public void setNonpersistent(String property, Object value) throws PropertyStoreException {
		store.setNonpersistent(property, value);
	}

	public Map<String,Object> getProperties() {
		return store.getProperties();
	}

	public Property getProperty(String key) {
		return store.getProperty(key);
	}

	protected PropertyConfiguration getPropertyConfiguration() {
		return store.getPropertyConfiguration();
	}
}
